package service;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import pojo.Animal;
import util.Spider;
import util.SpiderFileUtil;
import util.WxJsonUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AnimalDataService {

    Spider spider=new Spider();
    WxJsonUtil jsonUtil = new WxJsonUtil();
    String bWikiAdd="https://wiki.biligame.com";

    public void format(){
        System.out.println("**********执行开始**********");

        Document docment = null;
        try {
            docment = spider.getDocment(bWikiAdd+"/dongsen/%E5%B0%8F%E5%8A%A8%E7%89%A9%E5%9B%BE%E9%89%B4");
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(docment.html().length()>0?"收到html":"没有收到html");

        String title = docment.title();
        Element head = docment.head();


        Elements elementsByTag = docment.getElementById("CardSelectTr").getElementsByTag("tr");
        List<Animal> animals = formatToAnimals(elementsByTag);

        StringBuffer buffer = jsonUtil.arrayToJsonLineStr(animals);
        SpiderFileUtil.writeJSONFile("animal",buffer.toString());

        System.out.println("**********执行完成**********");

    }

    public List<Animal> formatToAnimals(Elements elements){
        List<Animal> animals = new ArrayList<Animal>();

        for (int i = 1; i <elements.size() ; i++) {
            Elements infoNodes = elements.get(i).getElementsByTag("td");
            Animal animal = formatToAnimal(infoNodes);
            animals.add(animal);
        }

        return animals;

    }

    public Animal formatToAnimal(Elements infoNodes){
        Animal animal = new Animal();

        animal.setName(infoNodes.get(0).getElementsByTag("a").first().attr("title"));
        animal.setImg(formatImgString(infoNodes.get(0)));
        animal.setSex(infoNodes.get(1).childNode(0).toString());
        animal.setDisposition(infoNodes.get(2).childNode(0).toString());
        animal.setBirthday(infoNodes.get(4).childNode(0).toString());
        animal.setPetPhrase(infoNodes.get(3).childNode(0).toString());

        System.out.println(animal.toString());
        return animal;
    }


    public String formatImgString(Element infoNodes){
        String img="";
        String nextAdd= infoNodes.getElementsByTag("a").first().attr("href");

        Document docment = null;
        try {
            docment = spider.getDocment(bWikiAdd + nextAdd);
        } catch (IOException e) {
            return "";
        }


        Elements nextImgDocs  =docment.getElementsByTag("img");

        for (int i = 0; i <nextImgDocs.size() ; i++) {
            if (nextImgDocs.get(i).className().equals("img-kk")){
                return  nextImgDocs.get(i).attr("src");
            }
        }

        return img;
    }

//    public void getDocment(String url){
//        return; spider.getDocment(url);
//
//    }

//    <V> V dataexist(Object obj){
//        if (obj==null){
//
//        }
//
//
//    }

}
